package com.bing.bingPro.service;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.extra.ssh.JschUtil;
import com.jcraft.jsch.Session;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@Service
@Slf4j
public class SendMailService {

    public void sendMailFunc(String recevier, String title, String meaildata) {
        //启动线程池
        ThreadUtil.execAsync(() -> {
            try {
                //调用方法
                sendMailThead(recevier, title, meaildata);
            } catch (Exception e) {
                e.printStackTrace();
            }
        },false);
    }
    /**
     * 发送邮件
     * @param recevier
     * @param title
     * @param meaildata
     * @return boolean
     */
    public void sendMailThead(String recevier, String title, String meaildata) throws InterruptedException {

            Assert.notBlank(recevier, "邮箱地址不能为空");
            Assert.notBlank(title, "邮件主题不能为空");
            Assert.notBlank(meaildata, "内容不能为空");
            int i = 0;
            boolean tag = true;
        Session session = null;
        while (tag){
                try {
                    i++;
                    log.info(String.format("邮件发送%s 次！", i));
                    //新建会话，此会话用于ssh连接到跳板机（堡垒机），此处为10.27.32.1
                    session = JschUtil.getSession("10.27.32.1", 22, "root", "g@uL9wuD");
                    session.setTimeout(300000); // 超时时间
//                    session.connect(60 * 1000); // 设置连接超时时间
                    log.info("登陆10.27.32.1成功");
                    String exec = JschUtil.exec(session, String.format("ssh root@10.243.67.96  python /home/jkzd/monitor/wyt/monitor_module.py %s %s %s", recevier, title, meaildata), StandardCharsets.UTF_8);
//                    String exec = JschUtil.exec(session, String.format("python /home/monitor_module.py %s %s %s", recevier, title, meaildata), StandardCharsets.UTF_8);
                    log.info("ssh10.243.67.96成功");
                    log.info(exec);

                    tag = false;

//                    session.disconnect(); // 断开连接
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error(e.getMessage());
                    log.error(String.format("邮件发送%s 次,失败！", i));
                    Thread.sleep(30000); // 延时30秒
                } finally {
                    JschUtil.close(session);
                }
                if (i == 3){
                    tag = false;
                }
            }
    }


    /**
     * 验证输入的邮箱格式是否符合
     * @param email
     * @return boolean
     */
    public static boolean isEmail(String email)
    {
        boolean tag = true;
        final String pattern1 = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
        final Pattern pattern = Pattern.compile(pattern1);
        final Matcher mat = pattern.matcher(email);
        if (!mat.find()) {
            tag = false;
        }
        return tag;
    }
}
